package com.xue.web.utils;

import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Item;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * MinIo工具类
 */
public class MinoIoUtil {

    //获取minio的连接
     MinioClient   minioClient;{
        try {
            //传入参数                                              ↓连接url                ↓账号              ↓密码
            minioClient = new MinioClient("http://106.15.55.45:9000", "minio", "minio123456");
        } catch (InvalidEndpointException | InvalidPortException e) {
            System.out.println("连接minio时发生异常"+e);
            throw new RuntimeException();
        }
    }
    /**查询文件夹是否存在
     *
     * @param folder minIo中的文件夹名
     * @return 真/假
     */
    public boolean getMinIoConnection(String folder){
        try {
            return minioClient.bucketExists(folder);
        } catch ( InvalidBucketNameException |
                 InsufficientDataException | ErrorResponseException | IOException | NoSuchAlgorithmException |
                 InvalidKeyException | InvalidResponseException | XmlParserException | InternalException e) {
            System.out.println("连接Minio时发生异常:"+e);
            return false;
        }
    }

    /**获取文件夹下所有文件名
     *
     * @param folder minio文件夹名称
     * @return  文件名集合
     */
    public List<String> getAllFile(String folder){
        List<String> listObjectNames = new ArrayList<>();
        Iterable<Result<Item>> myObjects ;
        try {
            myObjects = minioClient.listObjects(folder);
        } catch (XmlParserException e) {
            throw new RuntimeException(e);
        }
        for (Result<Item> result : myObjects) {
            Item item ;
            try {
                item = result.get();
            } catch (ErrorResponseException | InsufficientDataException | InternalException |
                     InvalidBucketNameException | InvalidKeyException | InvalidResponseException | IOException |
                     NoSuchAlgorithmException | XmlParserException e) {
                throw new RuntimeException(e);
            }
            listObjectNames.add(item.objectName());
        }
        return listObjectNames;
    }

    /**
     *
     * @param folder minio文件夹名称
     * @param fileName 上传文件名称
     * @param absolutePath  本地路径(绝对路径)
     * @return 成功/失败
     */
    public boolean uploadFiles(String folder,String fileName,String absolutePath){
        try {
            minioClient.putObject(folder, fileName,absolutePath ,null);
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidBucketNameException |
                 InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException |
                 XmlParserException e) {
            System.out.println("上传失败:"+e);
            return false;
        }
        System.out.println("上传成功");
        return true;
    }

}
